package Servlet.member;

import domain.News;
import service.NewsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/newData")
public class NewDetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置参数，默认值为1
        Long newDataId =1L;

        // 获取参数
        String id = request.getParameter("newDataId");
//        System.out.println("newDataId："+newDataId);

        // 当参数不为null时，将字符串id值转为Long数字类型值
        if (id!=null){
            newDataId = Long.parseLong(id);
        }

        // 得到已经赋值后的news对象
        News newsData = NewsService.getNewsById(newDataId);
        // 将news对象存储到域对象中
        request.setAttribute("newsData", newsData);
        // 跳转到../ind/content2.jsp页面中
        request.getRequestDispatcher("/ind/content2.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
